package com.sso.manager;

import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.sso.entity.*;
import com.sso.exception.SelectException;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import com.tools.util.StrUtils;
import com.sso.common.ConstantDate;
import com.sso.dao.SysRoleMapper;
import com.sso.entity.vo.SysRoleVo;

@Service
public class SysRoleManager {
	private Logger logger = LoggerFactory.getLogger(this.getClass());
	@Autowired
	private SysRoleMapper dao;
	@Autowired
	private SysMenuManager menuManager;
	@Autowired
	private SysMenuRoleManager menuRoleManager;
	@Autowired
	private SysUserRoleManager userRoleManager;
	@Autowired
	private SysUserManager userManager;

	@Transactional(rollbackFor=Exception.class)
	public void save(SysRole role) throws Exception {
		if (StrUtils.isEmpty(role.getId())) {
			role.setId(ConstantDate.getUUID());
			role.setCreateTime(new Date());
			role.setUpdateTime(new Date());
			role.setStatus(ConstantDate.RoleStatus.DISCARD.key);
			dao.insert(role);
		} else {
			role.setUpdateTime(new Date());
			dao.update(role);
		}
	}

	@Transactional(propagation = Propagation.SUPPORTS)
	public SysRole selectById(String id){
		Map<String, Object> filterMap = new HashMap<String, Object>();
		filterMap.put("id", id);
		return this.selectByUnique(filterMap);
	}

	@Transactional(propagation = Propagation.SUPPORTS)
	public List<SysRole> selectByProperty(Map<String, Object> filterMap){
		return dao.selectByProperty(filterMap);
	}

	@Transactional(propagation = Propagation.SUPPORTS)
	public List<SysRoleVo> selectVoByProperty(Map<String, Object> filterMap) throws SelectException {
		return dao.selectVoByProperty(filterMap);
	}

	@Transactional(propagation = Propagation.SUPPORTS)
	public Page<SysRoleVo> selectByPage(Map<String, Object> filterMap) {
		String pageNo = StrUtils.doNull((String)filterMap.get("pageNo"),Page.DEFAULT_PAGE_NO);
		String pageSize = StrUtils.doNull((String)filterMap.get("pageSize"),Page.DEFAULT_PAGE_SIZE);
		Page<SysRoleVo> page = new Page<SysRoleVo>(pageNo, pageSize);
		filterMap.put("first", page.getFirst());
		filterMap.put("last", page.getLast());
		List<SysRoleVo> list = dao.selectByPage(filterMap);
		long totalCount = dao.countByProperty(filterMap);
		page.setTotalCount(totalCount);
		page.setData(list);
		return page;
	}

	/**
	 * 保存角色权限方法
	 * @param roleid not null
	 * @param menuArray not null
	 * @throws Exception
     */
    @Transactional(rollbackFor = Exception.class)
	public void saveRoleAuth(String roleid,String[] menuArray) throws Exception {
		//清除原有角色权限
		menuRoleManager.deleteByRoleId(roleid);
		//角色权限
		for(String s : menuArray){
			SysMenu menu = menuManager.selectById(s);
			if(StrUtils.isEmpty(menu.getId())){
				continue;
			}
			SysMenuRole menuRole = new SysMenuRole(menu.getId(),roleid);
			menuRoleManager.save(menuRole);
		}
		//修改权限后清除所有用户权限信息
		this.userManager.clearAllUserAuth();;
	}

	/**
	 * 根据ID删除角色
	 * @param id
	 * @throws Exception
     */
	@Transactional(rollbackFor = Exception.class)
	public void deleteById(String id) throws Exception {
		//删除用户角色表权限数据
		userRoleManager.deleteByRoleId(id);
		//删除菜单角色表权限数据
		menuRoleManager.deleteByRoleId(id);
		//删除角色数据
		dao.delete(id);
		//删除角色后，清除所有用户信息
		this.userManager.clearAllUserAuth();
	}

	/**
	 * 唯一索引查询，要求查询条件必须唯一。
	 * @param param
	 * @return SysRole
     */
	@Transactional(propagation = Propagation.SUPPORTS)
	public SysRole selectByUnique(Map<String,Object> param){
		List<SysRole> list = this.selectByProperty(param);
		if (list.size() == 0) {
			return new SysRole();
		}
		if(list.size()>1){
			logger.info("SysRoleManager-->selectById查询错误..........");
			return new SysRole();
		}
		return list.get(0);
	}
}
